// 导出一个axios的实例  而且这个实例要有请求拦截器 响应拦截器
import axios from 'axios'
import store from '@/store'
import { jiami, jiemi } from "../utils/aeskey";
import { Toast } from 'vant'
import router from '../router'
// import { a } from './android.js'
import {
    getAndroid
} from './android'
// 创建一个axios的实例
const service = axios.create({
    // 设置基地址
    baseURL: 'https://app.buddyparty.xyz',
    withCredentials: true,
    timeout: 600000,
})
service.defaults.headers["Content-Type"] = "application/json";

// 请求拦截器
service.interceptors.request.use(
    async (config) => {
        //添加请求头
        const a = ''
        // console.log('token', t())
        // config.headers.Token = aGHTQZ5OZ973101ac048c021c4079118c2b7b7ec6c1dbbdbc4a6579db9f25ccf911bcc8207e34aef72bf0b3a0b0e15178f9ee3fa06899a02bc9f883d1f104d7138809c8bcfaf3fd33323e971be90c47100e30a01d26ab54301d0c044f78680365344b43126d64cf71f09805e012b1c0f6e1fa4991
        let token = await getAndroid();
        config.headers.Token = token;
        // console.log(token, '设备信息')
        // 判断用户是否登录添加token
        const auth = store.state.user.userInfo.token
        if (auth) {
            config.headers.Auth = auth
        }
        console.log(JSON.stringify(config.url), "ururlrjlkdjfksd");
        store.commit('showLoading')
        return config
    },
    (error) => {
        store.commit('showLoading')
        return Promise.reject(error)
    }

)

// 响应拦截器
service.interceptors.response.use(
    (response) => {
        // const result = jiemi(response.data)
        // if (result.state === 0) {

        // }
        if (response.data.status === 1012) {
            store.commit('user/clearuserInfo')
            router.push('/login')
        }
        console.log(JSON.stringify(response), "result");
        //对请求错误的结果进行拦截
        try {
            if (response.data.status && response.data.status !== 0) {
                Toast(response.data.message)
                store.commit('hideLoading')
                return
            }
        } catch (error) {

        }
        store.commit('hideLoading')
        return response
    },
    (error) => {
        store.commit('hideLoading')
        return Promise.reject(error)
    }
)

export default service // 导出axios实例